package com.wodsy.phoenix.datadao.orderc.dao.impl;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.orderc.dao.OrderDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.orderc.model.Order;
import com.wodsy.phoenix.dataobject.orderc.vo.BuyerOrderSearchReq;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * 名称：com.wodsy.orderc.datadao.dao.impl
 * 描述：<br>
 * 类型：JAVA<br>
 * 最近修改时间：2015-05-20 <br/>
 *
 * @author jiawenfeng
 * @since 2015-05-20
 */
@Repository("orderDao")
public class OrderDaoImpl extends BaseHibernateDao<Order, Long> implements OrderDao {
    @Override
    @Autowired
    @Qualifier(value="orderc")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public Class<Order> getModelClass() {
        return Order.class;
    }

    @Override
    public Pager<Order> findBySearchCondition(Pager<Order> orderPager, BuyerOrderSearchReq searchCondition) {
        Criteria criteria = getSession().createCriteria(getModelClass());

        if(searchCondition.getBuyerUserId() != null) {
            criteria.add(Restrictions.eq("buyerUserId", searchCondition.getBuyerUserId()));
        }
        criteria.addOrder(Property.forName("createTime").desc());

        return toPager(orderPager, criteria);
    }

    @Override
    public Order findByOrderId(Long orderId) {

        if(orderId == null) {
            return null;
        }

        return (Order)getSession().get(getModelClass(), orderId);

    }
}
